package com.example.toutiao.ui.main.act

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.base.Constants
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.presenter.RegisterPresenter
import com.example.toutiao.ui.main.view.RegisterView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.CommonUtils
import com.example.toutiao.utils.SharedPreferencesUtils
import kotlinx.android.synthetic.main.activity_set_new_pass.*

class SetNewPassActivity : BaseActivity<RegisterView, RegisterPresenter>(), RegisterView,
    TextWatcher {
    private var password:String =""
    private var phone:String=""
    override fun getLayoutId(): Int = R.layout.activity_set_new_pass

    override fun initView() {
        cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
        new_pass.addTextChangedListener(this)
        if (intent != null) {
            phone = CommonUtils.replaceBlank(intent.getStringExtra(Constants.MOBILE).toString())
        }
        cd_next.setOnClickListener {
            if(TextUtils.isEmpty(password)){
                Toast.makeText(application,"请输入正确的密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            getPresenter()!!.setPassword(phone,password)
        }
        icon_back.setOnClickListener {
            finish()
        }
    }

    override fun initData() {

    }

    override fun createPresenter(): RegisterPresenter =RegisterPresenter()



    override fun onRegister(registerModel: RegisterModel) {

    }

    override fun <T> setData(data: T) {
        if(data!=null){
            var result=data as RegisterModel
            SharedPreferencesUtils.saveToken(this, Constants.TOKEN, result.token)
            /*关闭之前存在的页面*/
            postMsg(EvtMsgModel(102, result.token))
            postMsg(EvtMsgModel(105, result.token))
            postMsg(EvtMsgModel(103, result.token))
            finish()
        }
    }

    override fun setError(err: String) {

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        password= CommonUtils.replaceBlank(s.toString())
        check()
    }

    private fun check(){
        if(password.length>=6){
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.main_color))
            cd_next.isEnabled=true
        }else{
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
            cd_next.isEnabled=false
        }
    }
    override fun afterTextChanged(s: Editable?) {

    }
    override fun isRegister(): Boolean =true


}